package me.firstapp.config;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import me.firstapp.spi.bbs.EmailService;
import me.firstapp.spi.bbs.MemberService;
import me.firstapp.spi.shop.PointRechargeCardService;
import me.firstapp.spi.shop.PointService;
import me.firstapp.spi.system.ApiClientService;
import me.firstapp.spi.system.EmailConfigService;
import me.firstapp.spi.system.OperatorService;
import me.firstapp.spi.zerone.CqsscService;

@Configuration
public class RootConfig {

	@Bean
	public PropertiesFactoryBean configProperties() {
		PropertiesFactoryBean configProperties = new PropertiesFactoryBean();
		configProperties.setLocations(new ClassPathResource("config.properties"));
		return configProperties;
	}

	@Bean
	public HttpInvokerProxyFactoryBean memberService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/member.service");
			proxy.setServiceInterface(MemberService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean pointRechargeCardService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/pointRechargeCard.service");
			proxy.setServiceInterface(PointRechargeCardService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean emailService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/email.service");
			proxy.setServiceInterface(EmailService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean pointService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/point.service");
			proxy.setServiceInterface(PointService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean apiClientService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/apiClient.service");
			proxy.setServiceInterface(ApiClientService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean emailConfigService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/emailConfig.service");
			proxy.setServiceInterface(EmailConfigService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean operatorService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/operator.service");
			proxy.setServiceInterface(OperatorService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean cqsscService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/cqssc.service");
			proxy.setServiceInterface(CqsscService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

}
